import java.util.LinkedList;
import java.util.Queue;

class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;

    TreeNode(int x) {
        val = x;
    }
}

// 使用队列
public class Codec {
    // Encodes a tree to a single string.
    public String serialize(TreeNode root) {
        StringBuilder buf = new StringBuilder();
        if (root == null) {
            buf.append("#,");
            return sb.toString();
        }
        sb.append(root.val + ",");
        sb.append(serialize(root.left));
        sb.append(serialize(root.right));

        return sb.toString(); // 经过上述步骤后，字符串中保存的是："1,2,#,3,#,#,4,5,#,#,#,"
    }

    // Decodes your encoded data to tree.
    int index = -1;

    public TreeNode deserialize(String data) {
        index++;
        String[] strr = data.split(",");
        TreeNode node = null;
        if (!strr[index].equals("#")) {
            node = new TreeNode(Integer.valueOf(strr[index]));
            node.left = deserialize(data);
            node.right = deserialize(data);
        }
        return node;

    }
}
